package com.shxy.entity;

import com.shxy.entity.common.enums.*;
import com.shxy.util.SimpleDate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Date;

/**
 * 员工信息实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tb_employee")
@SQLDelete(sql = "update tb_employee set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class Employee {
    @Id
    @GeneratedValue(generator = "jpa-uuid")
    @Column(length = 32)
    protected String id;                                    // 编号
    @Column(nullable = false, length = 32)
    protected String name;                                  // 姓名
    @Column(nullable = false, length = 32, unique = true)
    protected String idCard;                                // 身份证号码
    @Column(nullable = false)
    @Convert(converter = GenderEnum.Converter.class)
    protected GenderEnum gender;                            // 性别
    protected String department;                            // 所属部门
    private String post;                                    // 职务
    private boolean isDirector;                             // 是否主管

    @Convert(converter = EthicEnum.Converter.class)
    protected EthicEnum ethic;                              // 民族
    @Convert(converter = PoliticalEnum.Converter.class)
    protected PoliticalEnum political;                      // 政治面貌
    @Column(length = 32)
    protected String phone;                                 // 电话号码
    protected String address;                               // 家庭住址
    @Convert(converter = DistrictEnum.Converter.class)
    protected DistrictEnum nativePlace;                     // 籍贯
    private Date startWork;                                 // 参加工作时间
    private int salaryLevel;                                // 薪级

    private String account;                                 // 银行账号
    private String bank;                                    // 开户行

    private int deleted;                  // 删除标记

    /**
     * 获取出生日期
     * @return
     */
    public String getBirthday() {
        if(StringUtils.isNoneBlank(this.idCard)){
            return String.format("%s-%s-%s", this.idCard.substring(5, 8), this.idCard.substring(9, 10), this.idCard.substring(11, 12));
        }
        return null;
    }

    /**
     * 获取年龄
     * @return
     */
    public int getAge() {
        return SimpleDate.calculateAge(getBirthday());
    }
}
